package ch.ethz.inf.shop.model.simpleDatabase.operators.tests;

import java.text.ParseException;
import java.util.Comparator;

import junit.framework.Assert;

import org.junit.Test;

import ch.ethz.inf.shop.model.simpleDatabase.Tuple;
import ch.ethz.inf.shop.model.simpleDatabase.operators.Scan;
import ch.ethz.inf.shop.model.simpleDatabase.operators.Sort;

public class TestSort {
	@Test
	public void sortCommentAscendingTest() throws ParseException {

		Sort p = new Sort(new Scan(AllTests.ROOT_DIRECTORY + "/comment.txt", "comment"), new Comparator<Tuple>() {
			@Override
			public int compare(Tuple o1, Tuple o2) {
				return o1.getString("comment.text").toLowerCase().compareTo(o2.getString("comment.text").toLowerCase());
			}
		});

		Assert.assertTrue(p.moveNext());
		Assert.assertEquals(2, p.current().getInt(0));
		Assert.assertEquals(4, p.current().getInt(1));
		Assert.assertEquals(2, p.current().getInt(2));
		Assert.assertEquals("Diese Pizza schmeckt scheusslich. Ich will einen dislike button!", p.current().getString(3));
		Assert.assertEquals(Tuple.DATE_FORMAT.parseObject("2012-03-25 10:30:34"), p.current().getDate(4));

		Assert.assertTrue(p.moveNext());
		Assert.assertEquals("double like!", p.current().getString(3));
		Assert.assertTrue(p.moveNext());
		Assert.assertEquals("is there also a vegetarian pig?", p.current().getString(3));
		Assert.assertTrue(p.moveNext());
		Assert.assertEquals("papapapapaprika!", p.current().getString(3));
		Assert.assertTrue(p.moveNext());
		Assert.assertEquals("Wie sehr ich diesen Thun doch mag! Vor allem den leckeren Delfin darin!", p.current().getString(3));

		Assert.assertFalse(p.moveNext());

	}

	@Test
	public void sortShoppingCartDescendingTest() {
		Sort p = new Sort(new Scan(AllTests.ROOT_DIRECTORY + "/shopping_cart.txt", "shopping_cart"), new Comparator<Tuple>() {

			@Override
			public int compare(Tuple o1, Tuple o2) {
				return -o1.getString("shopping_cart.shopping_cart_id").compareToIgnoreCase(o2.getString("shopping_cart.shopping_cart_id"));
			}
		});

		Assert.assertTrue(p.moveNext());
		Assert.assertEquals(3, p.current().getInt(0));
		Assert.assertEquals(5, p.current().getInt(1));
		Assert.assertTrue(p.moveNext());
		Assert.assertEquals(2, p.current().getInt(0));
		Assert.assertEquals(2, p.current().getInt(1));
		Assert.assertTrue(p.moveNext());
		Assert.assertEquals(1, p.current().getInt(0));
		Assert.assertEquals(1, p.current().getInt(1));
		Assert.assertFalse(p.moveNext());

	}
}
